﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.EditorExt;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GlobeCore;
using ESRI.ArcGIS.NetworkAnalysis;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using DIWAGIS.Class;
using DIWAGIS.Modules;

namespace DIWAGIS.ApplicationLayer.QuanLyMangCapNuoc
{
    public partial class frmVanKhachHang : Form
    {
        public frmVanKhachHang()
        {
            InitializeComponent();
        }
        public IFeature pFeature;
        Boolean isLoadComBox = false;
        string sTableName = generalVariable.database + "." + generalVariable.schema + "." + ClassVariables.wVanKhachHang;
        private void frmVanKhachHangcs_Load(object sender, EventArgs e)
        {
            DIWAGIS.Module.WorkingWithDomain cbx = new DIWAGIS.Module.WorkingWithDomain();
            cbx.loadDataDomainToCombobox(Hieu, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMHieuVan");

            cbx.loadDataDomainToCombobox(NhaSanXuat, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMNhaSanXuatVan");
            cbx.loadDataDomainToCombobox(TinhTrang, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMTinhTrangVan");
            cbx.loadDataDomainToCombobox(NuocSanXuat, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMNuocSanXuatVan");
            cbx.loadDataDomainToCombobox(TieuChuan, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMTieuChuanVan");
            cbx.loadDataDomainToCombobox(CoVan, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMCoVanKhachHang");
            cbx.loadDataDomainToCombobox(ChieuDongVan, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMChieuDongVan");
            cbx.loadDataDomainToCombobox(VatLieu, DIWAGIS.Modules.generalVariable.ICurrentWorkspace, "DMVatLieuVan");
            cbx.LoadSubTypeToCombobox(LoaiVan, sTableName, "LoaiVan", DIWAGIS.Modules.generalVariable.ICurrentWorkspace);
            
            SqlCommand sqlcommand = new SqlCommand();
            //sqlcommand.CommandText = "SELECT TENHUYEN,IDHUYEN FROM " +  generalVariable.database + "." + generalVariable.schema + "." + " HUYEN ";
            sqlcommand.CommandText = "SELECT TENHUYEN,IDHUYEN FROM HUYEN ";
            cbx.loadDataToCombobox(MaQuan, sqlcommand);

            sqlcommand.CommandText = "SELECT TENCONDUONG,IDCONDUONG FROM " + generalVariable.database + "." + generalVariable.schema + "." + " CONDUONG ";
            cbx.loadDataToCombobox(MaDuong, sqlcommand);
            isLoadComBox = true;
            DIWAGIS.Class.general_Model p = new DIWAGIS.Class.general_Model();
            p.loadDataIntoForm(this, pFeature);
            if (pFeature.get_Value(pFeature.Fields.FindField("MaPhuong")) != null)
            {
                MaPhuong.SelectedValue = pFeature.get_Value(pFeature.Fields.FindField("MaPhuong"));
            }

        }

        private void MaQuan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isLoadComBox == true)
            {
                string strWhere = "";
                if (!string.IsNullOrEmpty(MaQuan.Text))
                {
                    strWhere = MaQuan.SelectedValue.ToString();
                    SqlCommand sqlcommand = new SqlCommand();
                    DIWAGIS.Module.WorkingWithDomain cbx = new DIWAGIS.Module.WorkingWithDomain();
                    //sqlcommand = new System.Data.SqlClient.SqlCommand();
                    sqlcommand.CommandText = "SELECT TenHanhChinh,IDHanhChinh FROM " + generalVariable.database + "." + generalVariable.schema + "." + "HANHCHINH" + " WHERE MaHuyen = @MaHuyen";
                    sqlcommand.Parameters.AddWithValue("@MaHuyen", strWhere);
                    cbx.loadDataToCombobox(MaPhuong, sqlcommand);
                    //loadDataToCombobox(cbxMaPhuong, sqlcommand);

                }
            }
        }

        private void buttonLuu_Click(object sender, EventArgs e)
        {
            IWorkspaceEdit pWorkSpaceEdit;
            pWorkSpaceEdit = generalVariable.ICurrentWorkspace as IWorkspaceEdit;
            pWorkSpaceEdit.StartEditing(true);
            pWorkSpaceEdit.StartEditOperation();

            DIWAGIS.Class.general_Model p = new DIWAGIS.Class.general_Model();
            p.setDataIntoDatatable(this, pFeature);

            pFeature.Store();
            pWorkSpaceEdit.StopEditOperation();
            pWorkSpaceEdit.StopEditing(true);
            pWorkSpaceEdit = null;
            MessageBox.Show("Cập nhật thành công", generalVariable.nameProgram, MessageBoxButtons.OK);
            this.Close();
        }

        private void buttonDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
